package fragment;

import java.util.ArrayList;

import com.nineoldandroids.view.ViewPropertyAnimator;
import com.qzhy.female.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class MessageFragment extends Fragment implements OnClickListener{

	private TextView mTvTabReply;
	private TextView mTvTabSysReply;
	private View line;
	private ArrayList<Fragment> Messagefragments;
	private int line_width;
	private ViewPager viewPager;
	private TextView mTvTabChatReply;

	public MessageFragment() {
		// Required empty public constructor
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.fragment_mesage, null);
		InitTab(view);
		return view;
	}

	private void InitTab(View view) {
		mTvTabReply = (TextView) view.findViewById(R.id.tab_reply);
		mTvTabSysReply = (TextView) view.findViewById(R.id.tab_sys_reply);
		mTvTabChatReply = (TextView) view.findViewById(R.id.tab_chat);
		mTvTabReply.setOnClickListener(this);
		mTvTabChatReply.setOnClickListener(this);
		mTvTabSysReply.setOnClickListener(this);
		line = view.findViewById(R.id.line);
		Messagefragments = new ArrayList<Fragment>();
		Messagefragments.add(new ReplyFragment());
		Messagefragments.add(new ChatFragment());
		Messagefragments.add(new SysReplyFragment());
		line_width = getActivity().getWindowManager().getDefaultDisplay().getWidth()
				/ Messagefragments.size();
		line.getLayoutParams().width = line_width;
		line.requestLayout();
		viewPager = (ViewPager)view.findViewById(R.id.viewPager);
		viewPager.setAdapter(new FragmentStatePagerAdapter(
				getChildFragmentManager()) {
			@Override
			public int getCount() {
				return Messagefragments.size();
			}

			@Override
			public Fragment getItem(int arg0) {
				return Messagefragments.get(arg0);
			}
		});
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				changeState(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				float tagerX = arg0 * line_width + arg2 / Messagefragments.size();
				ViewPropertyAnimator.animate(line).translationX(tagerX)
						.setDuration(0);
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		
		mTvTabReply.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				viewPager.setCurrentItem(0);
			}
		});

		mTvTabChatReply.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				viewPager.setCurrentItem(1);
			}
		});
		mTvTabSysReply.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				viewPager.setCurrentItem(2);
			}
		});
		
	}
	
	private void changeState(int arg0) {
		if (arg0 == 0) {
			mTvTabReply.setTextColor(getResources().getColor(R.color.red));
			mTvTabSysReply.setTextColor(getResources().getColor(R.color.black));
			mTvTabChatReply.setTextColor(getResources().getColor(R.color.black));
			ViewPropertyAnimator.animate(mTvTabReply).scaleX(1.2f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabReply).scaleY(1.2f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabSysReply).scaleX(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabSysReply).scaleY(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabChatReply).scaleX(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabChatReply).scaleY(1.0f).setDuration(200);
		} else if(arg0 ==1) {
			mTvTabSysReply.setTextColor(getResources().getColor(R.color.black));
			mTvTabReply.setTextColor(getResources().getColor(R.color.black));
			mTvTabChatReply.setTextColor(getResources().getColor(R.color.red));
			ViewPropertyAnimator.animate(mTvTabReply).scaleX(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabReply).scaleY(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabSysReply).scaleX(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabSysReply).scaleY(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabChatReply).scaleX(1.2f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabChatReply).scaleY(1.2f).setDuration(200);
		}else if(arg0 ==2) {
			mTvTabSysReply.setTextColor(getResources().getColor(R.color.red));
			mTvTabReply.setTextColor(getResources().getColor(R.color.black));
			mTvTabChatReply.setTextColor(getResources().getColor(R.color.black));
			ViewPropertyAnimator.animate(mTvTabReply).scaleX(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabReply).scaleY(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabSysReply).scaleX(1.2f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabSysReply).scaleY(1.2f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabChatReply).scaleX(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(mTvTabChatReply).scaleY(1.0f).setDuration(200);
		}
	}

	@Override
	public void onClick(View v) {
	}
}